package com.android.namecard.util;

/* import����class */
import java.io.File;
import java.util.List;

import com.android.namecard.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/* 自定义Adapter，继承android.widget.BaseAdapter */
public class MyFileAdapter extends BaseAdapter
{
  /* 变量声明
     mIcon1：回到根目录的图片文件
     mIcon2：回到上一层的图片
     mIcon3：文件夹的图片文件
     mIcon4：文件的图片
  */
  private LayoutInflater mInflater;
  private Bitmap mIcon1;
  private Bitmap mIcon2;
  private Bitmap mIcon3;
  private Bitmap mIcon4;
  private List<String> items;
  private List<String> paths;
  /* MyAdapter构造方法，传入3个参数  */  
  public MyFileAdapter(Context context,List<String> it,List<String> pa)
  {
    /* 参数初始化*/
    mInflater = LayoutInflater.from(context);
    items = it;
    paths = pa;
    mIcon1 = BitmapFactory.decodeResource(context.getResources(),
                                          R.drawable.file_paranet);
    mIcon2 = BitmapFactory.decodeResource(context.getResources(),
                                          R.drawable.file_root);
    mIcon3 = BitmapFactory.decodeResource(context.getResources(),
                                          R.drawable.directory);
    mIcon4 = BitmapFactory.decodeResource(context.getResources(),
                                          R.drawable.file_doc);
  }
  
  /* 因继承BaseAdapter需要重写如下方法method */
  @Override
  public int getCount()
  {
    return items.size();
  }

  @Override
  public Object getItem(int position)
  {
    return items.get(position);
  }
  
  @Override
  public long getItemId(int position)
  {
    return position;
  }
  
  @Override
  public View getView(int position,View convertView,ViewGroup parent)
  {
    ViewHolder holder;
    
    if(convertView == null)
    {
      /* 用自定义的file_row作为Layout */
      convertView = mInflater.inflate(R.layout.file_row, null);
      /* 初始化holder与text与Picon */
      holder = new ViewHolder();
      holder.text = (TextView) convertView.findViewById(R.id.text);
      holder.icon = (ImageView) convertView.findViewById(R.id.icon);
      
      convertView.setTag(holder);
    }
    else
    {
      holder = (ViewHolder) convertView.getTag();
    }

    File f=new File(paths.get(position).toString());
    /* 设置回到根目录的文字和icon */
    if(items.get(position).toString().equals("b1"))
    {
      holder.text.setText("返回根目录 /");
      holder.icon.setImageBitmap(mIcon1);
    }
    /* 设置返回上一层目录的文字和icon */
    else if(items.get(position).toString().equals("b2"))
    {
      holder.text.setText("返回上一层目录 ..");
      holder.icon.setImageBitmap(mIcon2);
    }
    /* 设置文件或文件夹的文字和icon */
    else
    {
      holder.text.setText(f.getName());
      if(f.isDirectory())
      {
        holder.icon.setImageBitmap(mIcon3);
      }
      else
      {
        holder.icon.setImageBitmap(mIcon4);
      }
    }
    return convertView;
  }
  /* class ViewHolder */
  private class ViewHolder
  {
    TextView text;
    ImageView icon;
  }
}
